/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { appRoot, initByProject, initExecuteCommand, runHvigor, } from '../../base/src/common/common';
import { addModule } from '../src/common/common';
import * as path from 'path';

describe('test common options', () => {
  test.concurrent('test -v(--version)', async () => {
    await initByProject('common-test', async (rootProject) => {
      const packageJsonPath = path.resolve(appRoot(__dirname), 'package.json');
      const version = require(packageJsonPath).version;

      addModule(rootProject, 'module1');
      const commands = initExecuteCommand({
        v: true,
        tasks: ''
      });
      const result = await runHvigor(rootProject, commands);
      expect(result.stdout).toContain(`${version}`);

    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test version command', async () => {
    await initByProject('common-test', async (rootProject) => {
      const packageJsonPath = path.resolve(appRoot(__dirname), 'package.json');
      const version = require(packageJsonPath).version;

      addModule(rootProject, 'module1');
      const commands = initExecuteCommand({
        version: true,
        tasks: ''
      });
      const result = await runHvigor(rootProject, commands);
      expect(result.stdout).toContain(`CLI version: ${version}`);
      expect(result.stdout).toContain(`Local version: ${version}`);

    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test --m', async () => {
    await initByProject('common-test', async (rootProject) => {

      addModule(rootProject, 'module1');
      addModule(rootProject, 'module2');
      const commands = initExecuteCommand({
        mode: 'module',
        tasks: 'clean'
      });
      const result = await runHvigor(rootProject, commands);
      expect(result.stdout).toContain('BUILD SUCCESSFUL');
      expect(result.stdout).toContain('This is a clean task');
      expect(result.stdout).toContain('Finished :module1:clean...');
      expect(result.stdout).toContain('Finished :module2:clean...');

      const commands1 = initExecuteCommand({
        mode: 'project',
        tasks: 'clean'
      });
      const result1 = await runHvigor(rootProject, commands1);

      expect(result1.stdout).toContain('BUILD SUCCESSFUL');
      expect(result.stdout).toContain('This is a clean task');
      expect(result1.stdout).toContain('Finished ::clean...');
      const commands2 = initExecuteCommand({
        mode: undefined,
        tasks: 'clean'
      });
      const result2 = await runHvigor(rootProject, commands2);
      expect(result2.stdout).toContain('BUILD SUCCESSFUL');
      expect(result.stdout).toContain('This is a clean task');
      expect(result.stdout).toContain('Finished :module1:clean...');
      expect(result.stdout).toContain('Finished :module2:clean...');
      expect(result2.stdout).toContain('Finished ::clean...');

    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test --no-parallel --no-incremental', async () => {
    await initByProject('common-test', async (rootProject) => {
      const commands = initExecuteCommand({
        noParallel: true,
        noIncremental: true,
        tasks: ''
      });
      const result = await runHvigor(rootProject, commands);
      expect(result.stdout).toContain('parallelExecution: false');
      expect(result.stdout).toContain('incrementalExecution: false');
    }, undefined, appRoot(__dirname));
  });
});
